<?php
namespace app\admin\controller;

class Cate extends Base
{
	public $name="cate";
    public function index()
    {
        $map=[];
        $list = db($this->name)->where($map)->orderRaw("sort=0,sort,id")->select();
		$list=list_to_tree($list);
        $this->assign('list', $list);
        \cookie('url', \request()->url());
        return $this->fetch();
    }
	public function post()
	{
	    $map=[];
	    $list = db($this->name)->where($map)->orderRaw("sort=0,sort,id")->select();
		$list=list_to_tree($list);
	    $this->assign('list', $list);
	    \cookie('url', \request()->url());
	    return $this->fetch();
	}
    public function add()
    {
        if ($this->request->isAjax()) {
			$data=input('param.');
			$cate=model($this->name);
			if(empty($data['link'])){
				if(empty($data['letter'])){
					$pinyin = new \Overtrue\Pinyin\Pinyin();
					$data['letter']=$pinyin->permalink($data['name'],'');
				}
				$data['url']='/'.$data['letter'].'/';
			}else{
				$data['letter']='';
				$data['url']=$data['link'];
			}
			$cate->allowField(true)->save($data);
 
            $this->success("添加成功", cookie('url'));
        }
		$mode=db('mode')->orderRaw("sort=0,sort,id")->select();
		$this->assign('mode',$mode);
        return $this->fetch();
    }
       
    public function edit()
    {
        $map['id']=input('id');
        $vo = db($this->name)->where($map)->find();
        if ($this->request->isAjax()) {
           $data=input('post.');
			if(empty($data['link'])){
				if(empty($data['letter'])){
					$pinyin = new \Overtrue\Pinyin\Pinyin();
					$data['letter']=$pinyin->permalink($data['name'],'');
				}
				$data['url']='/'.$data['letter'].'/';
			}else{
				$data['letter']='';
				$data['url']=$data['link'];
			}
			$cate=model($this->name);
			$cate->allowField(true)->save($data,['id'=>$vo['id']]);
			 
            $this->success("操作成功", cookie('url'));
        }
        $mode=db('mode')->orderRaw("sort=0,sort,id")->select();
        $this->assign('mode',$mode);
        $this->assign("vo", $vo);
        return $this->fetch();
    }
	public function page()
	{
	    $map['id']=input('id');
	    $vo = db($this->name)->where($map)->find();
	    if ($this->request->isAjax()) {
	       $data=input('post.');
			if(empty($data['link'])){
				if(empty($data['letter'])){
					$pinyin = new \Overtrue\Pinyin\Pinyin();
					$data['letter']=$pinyin->permalink($data['name'],'');
				}
				$data['url']='/'.$data['letter'].'/';
			}else{
				$data['letter']='';
				$data['url']=$data['link'];
			}
			$cate=model($this->name);
			$cate->allowField(true)->save($data,['id'=>$vo['id']]);
	        $this->success("操作成功", cookie('url'));
	    }
	    $mode=db('mode')->orderRaw("sort=0,sort,id")->select();
	    $this->assign('mode',$mode);
	    $this->assign("vo", $vo);
	    return $this->fetch();
	}
    public function dele()
    {
        db($this->name)->where('id', input('id'))->delete();
        $this->success("操作成功", cookie('url'));
    }
	function baidu(){
		$domain= \request()->domain();
		$urls=[];
		$urls[]=$domain;
		foreach(db('cate')->select() as $cate){
			if(empty($cate['link'])){
				$urls[]=$domain.$cate['url'];
				if($cate['mid']>1){
					foreach(db('post')->where('cid',$cate['id'])->order('id desc')->limit(500)->select() as $info){
						$urls[]=$domain.$info['url'];
					}
				}
			}
		}
		$res=\baidu_push($urls);
	 
		if(empty($res)){
			$this->error("请先设置百度推送API");
		}elseif(!empty($res['error'])){
			$this->error($res['error']);
		}else{
			$this->success("推送成功".$res['success']."条");
		}
	}
	function sitemap(){
		$domain= \request()->domain();
		$str=$domain.PHP_EOL;
		foreach(db('cate')->select() as $cate){
			if(empty($cate['link'])){
				$str.=$domain.$cate['url'].PHP_EOL;
				if($cate['mid']>1){
					foreach(db('post')->where('cid',$cate['id'])->order('id desc')->limit(500)->select() as $info){
						$str.=$domain.$info['url'].PHP_EOL;
					}
				}
			}
		}
		\file_put_contents(env('root_path').'sitemap.txt',$str);
		$this->success("生成成功",'');
		
		
	}
}
